/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.digitalization.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.digitalization.config.ArchiveConfiguration;
import org.springblade.modules.digitalization.entity.ManageRsdawjEntity;
import org.springblade.modules.digitalization.mapper.ManageRsdawjMapper;
import org.springblade.modules.digitalization.service.IManageRsdawjService;
import org.springblade.modules.digitalization.vo.ManageRsdawjVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

/**
 * 档案存储页信息表-JAVA 服务实现类
 *
 * @author RS
 * @since 2023-01-07
 */
@Service
public class ManageRsdawjServiceImpl extends BaseServiceImpl<ManageRsdawjMapper, ManageRsdawjEntity> implements IManageRsdawjService {

	@Autowired
	private ArchiveConfiguration configuration;

	@Override
	public IPage<ManageRsdawjVO> selectManageRsdawjPage(IPage<ManageRsdawjVO> page, ManageRsdawjVO rsdawj) {
		return page.setRecords(baseMapper.selectManageRsdawjPage(page, rsdawj));
	}

	@Override
	public ResponseEntity<Resource> getImage(HttpServletRequest request, String type, String rsdawj000, String rsdawj001) throws IOException {
		ManageRsdawjEntity manageRsdawjEntity = baseMapper.selectOne(Wrappers.<ManageRsdawjEntity>lambdaQuery()
			.eq(ManageRsdawjEntity::getRsdawj000, rsdawj000)
			.eq(ManageRsdawjEntity::getRsdawj001, rsdawj001));
		if (Func.isNotEmpty(manageRsdawjEntity)) {
			String imgName = type.equals("hd") ? manageRsdawjEntity.getHqLocalPath()+File.separator+manageRsdawjEntity.getRsdawj004() : manageRsdawjEntity.getLocalPath()+File.separator+manageRsdawjEntity.getRsdawj003();
			// 解码文件名
			String fileName = URLDecoder.decode(imgName, "UTF-8");
			// 获取图片文件路径
			String filePath = configuration.getArchivePath() +  File.separator + fileName;
			Path path = Paths.get(filePath);
			if (!Files.exists(path)) {
				return new ResponseEntity<>(HttpStatus.NOT_FOUND);
			}
			Resource resource = new UrlResource(path.toUri());
			String contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
			if (StringUtils.isEmpty(contentType)) {
				contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
			}
			return ResponseEntity.ok()
				.contentType(MediaType.parseMediaType(contentType))
				.body(resource);
		}else{
			throw new ServiceException("错误");
		}

	}

    @Override
	@Transactional(rollbackFor = Exception.class)
    public boolean updateSorts(List<Long> ids) {
		int sortStart=1;
        for(Long id:ids){
			ManageRsdawjEntity manageRsdawjEntity = new ManageRsdawjEntity();
			manageRsdawjEntity.setId(id);
			manageRsdawjEntity.setRsdawj001(String.format("%03d", sortStart));
			manageRsdawjEntity.setSort(sortStart++);
			this.updateById(manageRsdawjEntity);
		}
		return true;
    }

    @Override
    public List<ManageRsdawjVO> getWjByA000(String a0000) {
		return this.baseMapper.getWjByA000(a0000);
    }


}
